package xalan.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class XMLUtils {
	public static Element getFirstElement(Element parent, String childElementName) {
		return (Element)parent.getElementsByTagName(childElementName).item(0);
	}
	
	public static String getElementsText(Element parent, String childName) {
		Element e = getFirstElement(parent, childName);
		if(e == null){
			//throw new Exception("Nie mozna znalezc elementu: " + parent.getNodeName() + "->" + childName);
			System.out.println("Nie mozna znalezc elementu: " + parent.getNodeName() + "->" + childName);
			return "";
		}
		return e.getTextContent();
	}
	
	public static String getAttributeText(Element e, String attributeName) {
		return e.getAttribute(attributeName).toString();
	}
	
	public static void writeDocumentToOutputStream(Document document, OutputStream out) throws TransformerFactoryConfigurationError, FileNotFoundException, TransformerException {
		Transformer xformer = TransformerFactory.newInstance().newTransformer();
		DOMSource source = new DOMSource(document);
		StreamResult result = new StreamResult(out);
		xformer.transform(source, result);
	}
	
	public static void writeDocumentToFile(Document document, String fileName) throws TransformerFactoryConfigurationError, FileNotFoundException, TransformerException {
		writeDocumentToOutputStream(document, new FileOutputStream(fileName));
	}
	
	public static Document newDocument() throws ParserConfigurationException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = factory.newDocumentBuilder();
		return documentBuilder.newDocument();
	}
	
	public static Document parse(String fileName) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = factory.newDocumentBuilder();
		return documentBuilder.parse(new File(fileName));
	}
	
	public static String getChildElementAttribute(Element e, String childElementName, String attributeName) {
		return getAttributeText(getFirstElement(e, childElementName), attributeName);
	}
	
	public static void newElement(Document document, Element parent, String elementName, String textContent) {
		Element imageSrcElement = document.createElement(elementName);
		imageSrcElement.setTextContent(textContent);
		parent.appendChild(imageSrcElement);
	}
}
